<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="public/css.css"> 
<h1>Tien_C12TDT</h1>
<?php
require_once 'model/db.php';

$query = "SELECT * 
      FROM pages LIMIT 25 ";
$query1 = "SELECT * FROM pages ";
$pages = getPages($query);
$query = "SELECT * FROM companies ";
$companies = getPages($query);
$query = "SELECT * FROM apps ";
$apps = getPages($query);
$keyword = NULL;
$ratingMin = NULL;
$ratingMax = NULL;
$Company = NULL;

$App = NULL;
?>

<form method="POST">
    Keyword  : <input type="text" name="keyword"><br>
    Company  :  <select name="Company">
        <option value="0"><?php echo "<-------Company------->" ?></option>
        <?php foreach ($companies as $company): ?>
       
            <option value="<?php echo $company['id'] ?>"><?php echo $company["name"] ?></option>
        <?php endforeach; ?>
    </select> <br>
    App  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:  <select name="App">
        <option value="0"><?php echo "<-------App------->" ?></option>
        <?php foreach ($apps as $app): ?>
            <option value="<?php echo $app['pid'] ?>"><?php echo $app["name"] ?></option>
        <?php endforeach; ?>
    </select> <br>
    Min fans &nbsp;: <input type="text" name="ratingMin"><br>
    Max fans : <input type="text" name="ratingMax"><br>
    <input type="submit" name="submit" value="Search">
</form>
<?php if (isset($_POST['submit'])): ?>
    <?php $keyword = $_POST['keyword']; ?>
    <?php $ratingMin = (double) $_POST['ratingMin']; ?>
    <?php $ratingMax = (double) $_POST['ratingMax']; ?>
    <?php $Company = $_POST['Company']; ?>
    <?php $App = $_POST['App']; ?>
<?php endif; ?>

<table id="table" border="1px" style =" border-color:  #ff00cc">
    <tr class="bbb">
        <td class="pid">STT </td>
        <td class="id_page">id_page </td>
        <td class="name">name </td>
        <td class="company">company </td>
        <td class="likes">likes </td>
        <td class="talking">talking</td>
        <td class="category">category </td> 
        <td class="link">link </td>
        <td class="website">website </td>
        <td class="picture">picture </td>
        <td class="description">description </td>
    </tr>
    <?php foreach ($pages as $page): ?>
        <?php if ($Company == 0): ?>
            <?php if (empty($keyword)): ?>
                <?php if (empty($ratingMin) && empty($ratingMax)): ?>
                    <tr>
                        <td><?php echo $page["pid"] ?></td>
                        <td><?php echo $page["id"] ?></td>
                        <td><?php echo $page["name"] ?></td>
                        
                        <td><?php echo $page["likes"] ?></td>
                        <td><?php echo $page["talking_about_count"] ?></td>
                        <td><?php echo $page["category"] ?></td>
                        <td><a href='<?php echo $page["link"] ?>'><?php echo substr($page["link"], 0, 40) ?></td>
                        <td><a href='<?php echo $page["website"] ?>'><?php echo substr($page["website"], 0, 40) ?></td>
                        <td><img src ='<?php echo $page["picture"] ?>'</td>
                        <td><?php echo substr($page['description'], 0, 200) ?></td>
                    </tr>
                <?php elseif (empty($ratingMax)): ?>
                    <?php if ($ratingMin <= (double) $page["likes"]): ?>
                        <tr>
                            <td><?php echo $page["pid"] ?></td>
                            <td><?php echo $page["id"] ?></td>
                            <td><?php echo $page["name"] ?></td>
                            <td><?php echo $page["likes"] ?></td>
                            <td><?php echo $page["talking_about_count"] ?></td>
                            <td><?php echo $page["category"] ?></td>
                            <td><a href='<?php echo $page["link"] ?>'><?php echo substr($page["link"], 0, 40) ?></td>
                            <td><a href='<?php echo $page["website"] ?>'><?php echo substr($page["website"], 0, 40) ?></td>
                            <td><img src ='<?php echo $page["picture"] ?>'</td>
                            <td><?php echo substr($page['description'], 0, 200) ?></td>
                        </tr>
                    <?php endif; ?>
                <?php else: ?>
                    <?php if (($ratingMin <= (double) $page["likes"]) && ($ratingMax > $page["likes"])): ?>
                        <tr>
                            <td><?php echo $page["pid"] ?></td>
                            <td><?php echo $page["id"] ?></td>
                            <td><?php echo $page["name"] ?></td>
                            <td><?php echo $page["likes"] ?></td>
                            <td><?php echo $page["talking_about_count"] ?></td>
                            <td><?php echo $page["category"] ?></td>
                            <td><a href='<?php echo $page["link"] ?>'><?php echo substr($page["link"], 0, 40) ?></td>
                            <td><a href='<?php echo $page["website"] ?>'><?php echo substr($page["website"], 0, 40) ?></td>
                            <td><img src ='<?php echo $page["picture"] ?>'</td>
                            <td><?php echo substr($page['description'], 0, 200) ?></td>
                        </tr>
                    <?php endif; ?>
                <?php endif; ?>
            <?php else: ?>
                <?php if (strstr($page["description"], $keyword)): ?>
                    <?php if (empty($ratingMin) && empty($ratingMax)): ?>
                        <tr>
                            <td><?php echo $page["pid"] ?></td>
                            <td><?php echo $page["id"] ?></td>
                            <td><?php echo $page["name"] ?></td>
                            <td><?php echo $page["likes"] ?></td>
                            <td><?php echo $page["talking_about_count"] ?></td>
                            <td><?php echo $page["category"] ?></td>
                            <td><a href='<?php echo $page["link"] ?>'><?php echo substr($page["link"], 0, 40) ?></td>
                            <td><a href='<?php echo $page["website"] ?>'><?php echo substr($page["website"], 0, 40) ?></td>
                            <td><img src ='<?php echo $page["picture"] ?>'</td>
                            <td><?php echo substr($page['description'], 0, 200) ?></td>
                        </tr>
                    <?php elseif (empty($ratingMax)): ?>
                        <?php if ($ratingMin <= (double) $page["likes"]): ?>
                            <tr>
                                <td><?php echo $page["pid"] ?></td>
                                <td><?php echo $page["id"] ?></td>
                                <td><?php echo $page["name"] ?></td>
                                <td><?php echo $page["likes"] ?></td>
                                <td><?php echo $page["talking_about_count"] ?></td>
                                <td><?php echo $page["category"] ?></td>
                                <td><a href='<?php echo $page["link"] ?>'><?php echo substr($page["link"], 0, 40) ?></td>
                                <td><a href='<?php echo $page["website"] ?>'><?php echo substr($page["website"], 0, 40) ?></td>
                                <td><img src ='<?php echo $page["picture"] ?>'</td>
                                <td><?php echo substr($page['description'], 0, 200) ?></td>
                            </tr>
                        <?php endif; ?>
                    <?php else: ?>
                        <?php if (($ratingMin <= (double) $page["likes"]) && ($ratingMax > $page["likes"])): ?>
                            <tr>
                                <td><?php echo $page["pid"] ?></td>
                                <td><?php echo $page["id"] ?></td>
                                <td><?php echo $page["name"] ?></td>
                                <td><?php echo $page["likes"] ?></td>
                                <td><?php echo $page["talking_about_count"] ?></td>
                                <td><?php echo $page["category"] ?></td>
                                <td><a href='<?php echo $page["link"] ?>'><?php echo substr($page["link"], 0, 40) ?></td>
                                <td><a href='<?php echo $page["website"] ?>'><?php echo substr($page["website"], 0, 40) ?></td>
                                <td><img src ='<?php echo $page["picture"] ?>'</td>
                                <td><?php echo substr($page['description'], 0, 200) ?></td>
                            </tr>
                        <?php endif; ?>
                    <?php endif; ?>
                <?php endif; ?> 
            <?php endif; ?> 


        <?php else: ?>

                            
            <?php if ($Company == $company['id']): ?>
                <?php if (empty($keyword)): ?>
                        
                    <?php if (empty($ratingMin) && empty($ratingMax)): ?>
                        <tr>
                            <td><?php echo $page["pid"] ?></td>
                            <td><?php echo $page["id"] ?></td>
                            <td><?php echo $page["name"] ?></td>
                            <td><?php echo $page["likes"] ?></td>
                            <td><?php echo $page["talking_about_count"] ?></td>
                            <td><?php echo $page["category"] ?></td>
                            <td><a href='<?php echo $page["link"] ?>'><?php echo substr($page["link"], 0, 40) ?></td>
                            <td><a href='<?php echo $page["website"] ?>'><?php echo substr($page["website"], 0, 40) ?></td>
                            <td><img src ='<?php echo $page["picture"] ?>'</td>
                            <td><?php echo substr($page['description'], 0, 200) ?></td>
                        </tr>
                    <?php elseif (empty($ratingMax)): ?>
                        <?php if ($ratingMin <= (double) $page["likes"]): ?>
                            <tr>
                                <td><?php echo $page["pid"] ?></td>
                                <td><?php echo $page["id"] ?></td>
                                <td><?php echo $page["name"] ?></td>
                                <td><?php echo $page["likes"] ?></td>
                                <td><?php echo $page["talking_about_count"] ?></td>
                                <td><?php echo $page["category"] ?></td>
                                <td><a href='<?php echo $page["link"] ?>'><?php echo substr($page["link"], 0, 40) ?></td>
                                <td><a href='<?php echo $page["website"] ?>'><?php echo substr($page["website"], 0, 40) ?></td>
                                <td><img src ='<?php echo $page["picture"] ?>'</td>
                                <td><?php echo substr($page['description'], 0, 200) ?></td>
                            </tr>
                        <?php endif; ?>
                    <?php else: ?>
                        <?php if (($ratingMin <= (double) $page["likes"]) && ($ratingMax > $page["likes"])): ?>
                            <tr>
                                <td><?php echo $page["pid"] ?></td>
                                <td><?php echo $page["id"] ?></td>
                                <td><?php echo $page["name"] ?></td>
                                <td><?php echo $page["likes"] ?></td>
                                <td><?php echo $page["talking_about_count"] ?></td>
                                <td><?php echo $page["category"] ?></td>
                                <td><a href='<?php echo $page["link"] ?>'><?php echo substr($page["link"], 0, 40) ?></td>
                                <td><a href='<?php echo $page["website"] ?>'><?php echo substr($page["website"], 0, 40) ?></td>
                                <td><img src ='<?php echo $page["picture"] ?>'</td>
                                <td><?php echo substr($page['description'], 0, 200) ?></td>
                            </tr>
                        <?php endif; ?>
                    <?php endif; ?>
                <?php else: ?>
                    <?php if (strstr($page["description"], $keyword)): ?>
                        <?php if (empty($ratingMin) && empty($ratingMax)): ?>
                            <tr>
                                <td><?php echo $page["pid"] ?></td>
                                <td><?php echo $page["id"] ?></td>
                                <td><?php echo $page["name"] ?></td>
                                <td><?php echo $page["likes"] ?></td>
                                <td><?php echo $page["talking_about_count"] ?></td>
                                <td><?php echo $page["category"] ?></td>
                                <td><a href='<?php echo $page["link"] ?>'><?php echo substr($page["link"], 0, 40) ?></td>
                                <td><a href='<?php echo $page["website"] ?>'><?php echo substr($page["website"], 0, 40) ?></td>
                                <td><img src ='<?php echo $page["picture"] ?>'</td>
                                <td><?php echo substr($page['description'], 0, 200) ?></td>
                            </tr>
                        <?php elseif (empty($ratingMax)): ?>
                            <?php if ($ratingMin <= (double) $page["likes"]): ?>
                                <tr>
                                    <td><?php echo $page["pid"] ?></td>
                                    <td><?php echo $page["id"] ?></td>
                                    <td><?php echo $page["name"] ?></td>
                                    <td><?php echo $page["likes"] ?></td>
                                    <td><?php echo $page["talking_about_count"] ?></td>
                                    <td><?php echo $page["category"] ?></td>
                                    <td><a href='<?php echo $page["link"] ?>'><?php echo substr($page["link"], 0, 40) ?></td>
                                    <td><a href='<?php echo $page["website"] ?>'><?php echo substr($page["website"], 0, 40) ?></td>
                                    <td><img src ='<?php echo $page["picture"] ?>'</td>
                                    <td><?php echo substr($page['description'], 0, 200) ?></td>
                                </tr>
                            <?php endif; ?>
                        <?php else: ?>
                            <?php if (($ratingMin <= (double) $page["likes"]) && ($ratingMax > $page["likes"])): ?>
                                <tr>
                                    <td><?php echo $page["pid"] ?></td>
                                    <td><?php echo $page["id"] ?></td>
                                    <td><?php echo $page["name"] ?></td>
                                    <td><?php echo $page["likes"] ?></td>
                                    <td><?php echo $page["talking_about_count"] ?></td>
                                    <td><?php echo $page["category"] ?></td>
                                    <td><a href='<?php echo $page["link"] ?>'><?php echo substr($page["link"], 0, 40) ?></td>
                                    <td><a href='<?php echo $page["website"] ?>'><?php echo substr($page["website"], 0, 40) ?></td>
                                    <td><img src ='<?php echo $page["picture"] ?>'</td>
                                    <td><?php echo substr($page['description'], 0, 200) ?></td>
                                </tr>
                            <?php endif; ?>
                        <?php endif; ?>
                    <?php endif; ?> 
                <?php endif; ?>
            <?php endif; ?>
        <?php endif; ?>
    <?php endforeach; ?>
   
</table>
 